package com.xxl.job.consume.repository;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.consume.common.RedisKeyConstant;
import com.xxl.job.consume.model.AlarmDefModel;
import com.xxl.job.consume.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询报警配置信息
 */
@Repository
public class AlarmDefRepository {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询多个报警信号配置
     * @param signalType
     * @return
     */
    public List<AlarmDefModel> query(String signalType) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.POW_DEF_ALARM+signalType);
        List<String> sigDefModelJsonList = valueOperations.values();
        List<AlarmDefModel> list=new ArrayList<>();
        if (sigDefModelJsonList != null && sigDefModelJsonList.size() > 0){
            for (String sigDefModelStr : sigDefModelJsonList) {
                if (StringUtils.isNotBlank(sigDefModelStr)) {
                    try {
                        AlarmDefModel alarmDefModel = JSONUtils.json2pojo(sigDefModelStr, AlarmDefModel.class);
                        list.add(alarmDefModel);
                    } catch (IOException e) {
                        XxlJobLogger.log("终端JSON='{}' 转换异常！", sigDefModelStr, e);
                    }
                }
            }
            return list;
        }else {
         //   XxlJobLogger.log("没有 signalCode='{}'对应的信号告警定义信息！",signalType);
            return null;
        }
    }

}
